package com.example.demo.controller;

import com.example.demo.bean.Order;
import com.example.demo.common.User;
import com.example.demo.listener.MyEvent;
import com.example.demo.quartz.QuartzManager;
import com.example.demo.service.Demo;
import com.example.demo.service.design.mode.IPayment;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@RestController
@RequestMapping("/api")
@EnableSwagger2
public class DemoController {

    @Autowired
    private Demo demo;

    @Autowired
    private ApplicationEventPublisher applicationEventPublisher;


    @Autowired
    private ApplicationContext applicationContext;

    @Autowired
    private QuartzManager quartzManager;


    @RequestMapping(value = "/firstTest", method = RequestMethod.GET)
    @ApiOperation(value = "firstTest", httpMethod = "GET")
    public String firstTest(@RequestParam String param, String id) {
        applicationEventPublisher.publishEvent(new MyEvent("触发监听器"));
        return param + demo.getUserById(id).getUserId();
    }


    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    @ApiOperation(value = "getUser", httpMethod = "GET")
    public User getUser(String id) {
        return demo.getUserById(id);
    }


    @RequestMapping(value = "/paymentTest", method = RequestMethod.GET)
    @ApiOperation(value = "策略模式测试", httpMethod = "GET")
    @ApiImplicitParam(name = "paymentType", value = "Alipay|UnionPay|WechatPay", required = true, dataType = "String", paramType = "query")
    public String paymentTest(@RequestParam("paymentType") String paymentType) {

        Order order = Order.builder().paymentType(paymentType).build();
        IPayment payment = applicationContext.getBean(paymentType, IPayment.class);
        return payment.pay(order);
    }


    @RequestMapping(value = "/schedulerTest", method = RequestMethod.GET)
    @ApiOperation(value = "定时任务测试", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "王二", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "text", value = "测试", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "date", value = "yyyy-MM-dd HH:mm:ss", required = true, dataType = "String", paramType = "query")
    })
    public void schedulerTest(String name, String text, String date) throws SchedulerException {

        quartzManager.startJob(name, text, date);

    }


}
